#!/usr/bin/env python
# -*- coding:utf-8 -*-

from django.shortcuts import HttpResponse,render,redirect
from app01.forms import home as HomeForm
from app01 import models

def login_auth(func):
    def wrapper(request, *args, **kwargs):
        result = request.session.get('is_login', False)
        if not result:
            return redirect('/login/')
        # result = json.loads(result)
        # request.username = result['username']
        response = func(request, *args, **kwargs)
        return response
    return wrapper

@login_auth
def index(request):
    hosts = models.Host.objects.all().values()
    print hosts
    for line in hosts:
        '''
        遍历主机表的每条数据
        把hostgroup_id和值==>hostgroup和对应的值
        添加业务线和对应的值到字典
        将字典hosts传给前端
        '''
        hostgroup_line = models.HostGroup.objects.filter(id=line['hostgroup_id']).values() #[{'hostgroup': u'\u6d4b\u8bd5'}]
        hostgroup_val = hostgroup_line[0]['hostgroup']
        del line['hostgroup_id']
        line['hostgroup'] = hostgroup_val
        #business-->line
        business_id = hostgroup_line[0]['business_id']
        business_val = models.Business.objects.filter(id=business_id).values('business')[0]['business']
        line['business']=business_val
    return render(request,'home/index.html',{'hosts':hosts})

@login_auth
def create_host(request):
    create_obj = HomeForm.CreateHost(request.POST)
    if request.method == "POST":
        if create_obj.is_valid():
            all_data = create_obj.clean()
            models.Host.objects.create(**all_data)
            return redirect('/index/')
        else:
            error=create_obj.errors
            return render(request,'home/create_host.html',{'create_obj':create_obj,'error':error})
    return render(request,'home/create_host.html',{'create_obj':create_obj,})

@login_auth
def del_host(request):
    del_obj = HomeForm.DelHost(request.POST)
    if request.method == "POST":
        if del_obj.is_valid():
            all_data = del_obj.clean()
            models.Host.objects.filter(**all_data).delete()
            return redirect('/index/')
        else:
            error=del_obj.errors
            print error
            return render(request,'home/del_host.html',{'del_obj':del_obj,'error':error})
    return render(request,'home/del_host.html',{'del_obj':del_obj})

def update_host(request):
    update_obj = HomeForm.UpdateHost(request.POST)
    if request.method == "POST":
        if update_obj.is_valid():
            all_data = update_obj.clean()
            '''
            hostid是必填项
            获取到的数据，如果字段为空，就把这个item删除，保证字典都有数据
            按hostid为查询条件，整个字典数据传入到修改值
            '''
            for key,val in all_data.items():
                if not val:
                    del all_data[key]
            print all_data
            models.Host.objects.filter(hostid=all_data['hostid']).update(**all_data)
            return redirect('/index/')
        else:
            error=update_obj.errors
            print error
            return render(request,'home/update_host.html',{'update_obj':update_obj,'error':error})
    return render(request,'home/update_host.html',{'update_obj':update_obj})